<template>

  <div class="common-layout">
    <el-container>
      <el-header>
        <span>{{nickName}}(角色:{{ roleNames }})</span>
      </el-header>
      <el-container>

        <!-- 左边栏菜单 -->
        <el-aside width="200px">
          <el-menu
              class="el-menu-vertical-demo"
              default-active="2"
              active-text-color="#ffd04b"
              background-color="#545c64"
              text-color="#fff"
              @open="handleOpen"
              @close="handleClose"
              @select="handleLeftSelect"
            >
              <el-sub-menu v-for="item in menuTree" :key="item.menuId" :index="item.menuName">
                <template #title>
                  <el-icon><location /></el-icon>
                  <span>{{item.menuName}}</span>
                </template>
                <el-menu-item v-for="it in item.children" :key="it.menuId" :index="it.menuPath">{{it.menuName}}</el-menu-item>
              </el-sub-menu>

              
              

            </el-menu>
        </el-aside>

        <!-- 右侧主页面 -->
        <el-main>
          <router-view/>
        </el-main>

      </el-container>
    </el-container>
  </div>

</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue';
import {
  Document,
  Menu as IconMenu,
  Location,
  Setting,
} from '@element-plus/icons-vue'
import { useRouter } from 'vue-router';
import { getMyRoleList,getUserMenuTree } from '@/api/api'


const router = useRouter();
const activeIndex2 = ref('1')

const nickName = ref('');
const roleList = ref([]);
const roleNames = ref('')
const menuTree = ref([]);

const handleSelect = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}

// 左侧菜单的点击事件
const handleLeftSelect = (key: string, keyPath: string[]) => {
  console.log(key, keyPath);
  if(key == 'user') {
    router.push('/home/user');
  }
  else if(key == 'my') {
    router.push('/home/my');
  }
  else if(key == 'prod') {
    router.push('/home/prod');
  }
  else if(key == 'order') {
    router.push('/home/order');
  }
  else if(key == 'useres') {
    router.push('/home/useres');
  }
  else if(key == 'log') {
    router.push('/home/log');
  }
}

const handleOpen = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}
const handleClose = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}

// 页面加载钩子函数
onMounted(() => {
  // 回显当前用户的真实姓名
  let u = sessionStorage.getItem('user');
  if(u == null) {
    router.push('/login');
    return;
  }
  let user = JSON.parse(u);
  nickName.value = user.nickName

  // 获取权限列表
  getMyRoleList().then(res => {
    if(res.data.code == 200) {
      roleList.value = res.data.data;
      console.log('roleList', roleList.value);

      roleNames.value = roleList.value.join(',');
      
    }
  })

  // 获取当前用户的菜单树
  getUserMenuTree().then(res => {
    console.log('tree', res);
    if(res.data.code == 200) {
      menuTree.value = res.data.data;
    }
    
  })
}) 
</script>

<style scoped>

</style>